/*
 * lcd.h
 *
 * Created: 06.10.2013
 *  Author: qw3r
 */


#ifndef LCD_H_
#define LCD_H_


/* Hardware pin definitions */
//  LCD DB4-DB7 <-->  PORTD Bit PD0-PD3
#define LCD_PORT      PORTD
#define LCD_DDR       DDRD
#define LCD_DB        PD0

//  LCD RS      <-->  PORTD Bit PD4     (RS: 1=Data, 0=Command)
#define LCD_RS_PORT   PORTB
#define LCD_RS_DDR	  DDRB
#define LCD_RS        PB6

//  LCD EN      <-->  PORTD Bit PD5     (EN: 1-Impuls f?r Daten)
#define LCD_EN_PORT   PORTB
#define LCD_EN_DDR    DDRB
#define LCD_EN        PB5


/* Timeouts definitions */
// Enable pulse timeouts
#define LCD_ENABLE_DELAY_US_1	1		// >140ns ??
#define LCD_ENABLE_DELAY_US_2	50		// >37us  ??

// Init timeouts
#define LCD_POWERON_DELAY_MS	50

// Common timeouts
#define LCD_WRITEDATA_US        45
#define LCD_COMMAND_US          50

#define LCD_CLEAR_DISPLAY_MS    2
#define LCD_CURSOR_HOME_MS      2


/* LCD control definitions */
// Clear Display -------------- 0b00000001
#define LCD_CLEAR_DISPLAY       0x01

// Cursor Home ---------------- 0b0000001x
#define LCD_CURSOR_HOME         0x02

// Set Entry Mode ------------- 0b000001xx
#define LCD_SET_ENTRY           0x04

#define LCD_ENTRY_DECREASE      0x00
#define LCD_ENTRY_INCREASE      0x02
#define LCD_ENTRY_NOSHIFT       0x00
#define LCD_ENTRY_SHIFT         0x01

// Set Display ---------------- 0b00001xxx
#define LCD_SET_DISPLAY         0x08

#define LCD_DISPLAY_OFF         0x00
#define LCD_DISPLAY_ON          0x04
#define LCD_CURSOR_OFF          0x00
#define LCD_CURSOR_ON           0x02
#define LCD_BLINKING_OFF        0x00
#define LCD_BLINKING_ON         0x01

// Set Shift ------------------ 0b0001xxxx
#define LCD_SET_SHIFT           0x10

#define LCD_CURSOR_MOVE         0x00
#define LCD_DISPLAY_SHIFT       0x08
#define LCD_SHIFT_LEFT          0x00
#define LCD_SHIFT_RIGHT         0x04

// Set Function --------------- 0b001xxxxx
#define LCD_SET_FUNCTION        0x20

#define LCD_FUNCTION_4BIT       0x00
#define LCD_FUNCTION_8BIT       0x10
#define LCD_FUNCTION_1LINE      0x00
#define LCD_FUNCTION_2LINE      0x08
#define LCD_FUNCTION_5X7        0x00
#define LCD_FUNCTION_5X10       0x04


#define LCD_SET_DDADR           0x80
#define LCD_DDADR_LINE1         0x00
#define LCD_DDADR_LINE2         0x40
#define LCD_DDADR_LINE3         0x10
#define LCD_DDADR_LINE4         0x50


/* Functions prototypes */

void lcd_init( void );
void lcd_clear( void );
void lcd_command( uint8_t data );
void lcd_data( uint8_t data );
void lcd_string( char *data );
void lcd_setcursor( uint8_t x, uint8_t y );
void lcd_cursor_home( void );

#endif /* LCD_H_ */